package com.hongxun.budget.ui.dialog;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.adapter.SpinnerAdapter;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.entity.LoginInfo;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.ys.EDrawLineEntity;
import com.hongxun.interfaces.ResponseListener;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/29.
 */

public class PullLineYsDialog extends BaseCenterFragmentDialog {


    @BindView(R.id.distance)
    EditText distance;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.height)
    EditText height;
    @BindView(R.id.delete)
    Button delete;
    ResponseListener listener;
    @BindView(R.id.spinner2)
    Spinner spinner2;
    SpinnerAdapter<BDataDictionaryEntity> adapter1;
    SpinnerAdapter<BDataDictionaryEntity> adapter2;

    EDrawLineEntity entity;//修改的

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_ys_pullline;
    }

    @Override
    protected void initViews() {
        List<BDataDictionaryEntity> line_type = DBUtils.getBDataDictionaryEntities("line_type");
        List<BDataDictionaryEntity> line_soil = DBUtils.getBDataDictionaryEntities("line_soil");
        spinner.setAdapter(adapter1 = new SpinnerAdapter<BDataDictionaryEntity>(context, line_type, spinner));
        spinner2.setAdapter(adapter2 = new SpinnerAdapter<BDataDictionaryEntity>(context, line_soil, spinner));
        if (entity != null) {
            delete.setVisibility(View.VISIBLE);
            distance.setText(entity.getDistance() + "");
            height.setText(entity.getDLDeep() + "");
            int i = 0;
            for (BDataDictionaryEntity b : line_type) {
                if (entity.getDLType().equals(b.getDValue())) {
                    spinner.setSelection(i);
                    break;
                }
                i++;
            }
            i = 0;
            for (BDataDictionaryEntity b : line_soil) {
                if (entity.getDLSoil().equals(b.getDValue())) {
                    spinner.setSelection(i);
                    break;
                }
                i++;
            }
        }
    }

    @OnClick({R.id.cancel, R.id.confirm, R.id.delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delete:
                listener.response("delete");
                break;
            case R.id.confirm:
                String h = height.getText().toString();
                String ds = distance.getText().toString();
                if (TextUtils.isEmpty(h) || h.equals("0")) {
                    T.showLong(context, "坑深不能为空或0");
                    return;
                }
                if (TextUtils.isEmpty(ds) || ds.equals("0")) {
                    T.showLong(context, "与杆塔的距离不能为空或0");
                    return;
                }
                if (entity == null) {
                    EDrawLineEntity d = new EDrawLineEntity();
                    d.setGuid(UUID.randomUUID().toString());
                    d.setPGuid((String) SPUtils.get(context, "project_ys_guid", ""));
                    d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
                    d.setDistance(Double.parseDouble(ds));
                    d.setDLDeep(Double.parseDouble(h));
                    d.setDLType(adapter1.getSelectedItem().getDValue());
                    d.setDLSoil(adapter2.getSelectedItem().getDValue());
                    d.setDLUserid(LoginInfo.getInfo(context).getUserId());
                    d.setDLDatastate("新增");
                    d.setDLCollecttime(new Date());
                    listener.response(d);
                } else {
                    entity.setDistance(Double.parseDouble(ds));
                    entity.setDLDeep(Double.parseDouble(h));
                    entity.setDLType(adapter1.getSelectedItem().getDValue());
                    entity.setDLSoil(adapter2.getSelectedItem().getDValue());
                    entity.setDLUserid(LoginInfo.getInfo(context).getUserId());
                    entity.setDLDatastate("修改");
                    listener.response(entity);
                }
                break;
        }
        dismiss();
    }

    public PullLineYsDialog setListener(ResponseListener listener) {
        this.listener = listener;
        return this;
    }

    public PullLineYsDialog setEntity(EDrawLineEntity entity) {
        this.entity = entity;
        return this;
    }
}
